//
//  SReactViewManager.swift
//  RN
//
//  Created by HFY on 2021/1/19.
//

import Foundation

class SReactViewManager {
    
    static let shared:SReactViewManager = SReactViewManager()
    
    private var mapTable = NSMapTable<NSString,SReactViewController>.strongToWeakObjects()
    
    /// RN app实例 唯一标识
    func generateRNID() -> String{
        return UUID.init().uuidString
    }
    
    func getRN(rnID:String) ->  SReactViewController?{
        return self.mapTable.object(forKey: rnID as NSString)
    }
    
    func add(rn:SReactViewController) {
        let key = generateRNID()
        mapTable.setObject(rn, forKey: key as NSString)
    }
    
    /// 获取rn ID
    /// - Parameter rn: rn
    /// - Returns: id
    func getRNID(rn:SReactViewController) -> String? {
        var value:SReactViewController? = nil
        var id:String? = nil
        while value == nil,let key = mapTable.keyEnumerator().nextObject() {
            value = mapTable.object(forKey: key as? NSString)
            id = key as? String
        }
        return id
    }
    
    
}
